package com.standard.logcat.adapter

import android.content.ClipboardManager
import android.content.Context.CLIPBOARD_SERVICE
import android.content.Intent
import android.widget.TextView
import android.widget.Toast
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.standard.logcat.R
import com.standard.logcat.entity.LogcatItem
import com.standard.logcat.logBus.LogBus
import com.standard.logcat.provider.Init
import java.util.*


class LogcatAdapter(layoutResId: Int = R.layout.logcat_item, data: LinkedList<LogcatItem>) :
    BaseQuickAdapter<LogcatItem, BaseViewHolder>(layoutResId, data = data) {

    override fun convert(holder: BaseViewHolder, item: LogcatItem) {
        val log = holder.getView<TextView>(R.id.tv_log)

        log.setTextColor(log.context.resources.getColor(item.color))
        log.text = item.log

        log.setOnClickListener {
            val intent = Intent()
            intent.action = Intent.ACTION_SEND
            intent.putExtra(Intent.EXTRA_TEXT, item.log)
            intent.putExtra(Intent.EXTRA_SUBJECT, "分享日志")
            intent.type = "text/url"
            val shareIntent = Intent.createChooser(intent, "分享日志")
            Init.getActivity()?.startActivity(shareIntent)
            LogBus.hideLog.value = true
        }

        log.setOnLongClickListener {
            val cmb = context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager?
            cmb?.text = item.log
            Toast.makeText(context, "已复制到剪切板", Toast.LENGTH_LONG).show()
            true
        }
    }
}